<template>
  <div class="app-container">
    <h2>打印机序列号管理功能测试页面</h2>
    
    <el-card class="box-card" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span>功能测试</span>
      </div>
      <el-button type="primary" @click="testList">测试列表查询</el-button>
      <el-button type="success" @click="testAdd">测试新增</el-button>
      <el-button type="warning" @click="testUpdate">测试修改</el-button>
      <el-button type="danger" @click="testDelete">测试删除</el-button>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>测试结果</span>
      </div>
      <pre>{{ testResult }}</pre>
    </el-card>
  </div>
</template>

<script>
import { listPrinterSerialNumber, addPrinterSerialNumber, updatePrinterSerialNumber, delPrinterSerialNumber } from "@/api/monitor/njt/printer/serialNumber";

export default {
  name: "PrinterSerialNumberTest",
  data() {
    return {
      testResult: "点击按钮开始测试..."
    }
  },
  methods: {
    async testList() {
      try {
        this.testResult = "正在测试列表查询...";
        const response = await listPrinterSerialNumber({
          pageNum: 1,
          pageSize: 10
        });
        this.testResult = JSON.stringify(response, null, 2);
        this.$message.success("列表查询测试成功");
      } catch (error) {
        this.testResult = "列表查询测试失败: " + error.message;
        this.$message.error("列表查询测试失败");
      }
    },
    
    async testAdd() {
      try {
        this.testResult = "正在测试新增...";
        const data = {
          serialNumber: "TEST" + Date.now(),
          printerType: "portable",
          usageStatus: "unused",
          remark: "测试数据"
        };
        const response = await addPrinterSerialNumber(data);
        this.testResult = JSON.stringify(response, null, 2);
        this.$message.success("新增测试成功");
      } catch (error) {
        this.testResult = "新增测试失败: " + error.message;
        this.$message.error("新增测试失败");
      }
    },
    
    async testUpdate() {
      try {
        this.testResult = "正在测试修改...";
        // 先获取一个存在的记录
        const listResponse = await listPrinterSerialNumber({ pageNum: 1, pageSize: 1 });
        if (listResponse.rows && listResponse.rows.length > 0) {
          const record = listResponse.rows[0];
          const data = {
            id: record.id,
            serialNumber: record.serialNumber,
            printerType: record.printerType,
            usageStatus: record.usageStatus === 'used' ? 'unused' : 'used',
            remark: record.remark + " - 测试修改"
          };
          const response = await updatePrinterSerialNumber(data);
          this.testResult = JSON.stringify(response, null, 2);
          this.$message.success("修改测试成功");
        } else {
          this.testResult = "没有数据可供修改测试";
        }
      } catch (error) {
        this.testResult = "修改测试失败: " + error.message;
        this.$message.error("修改测试失败");
      }
    },
    
    async testDelete() {
      try {
        this.testResult = "正在测试删除...";
        // 先获取一个存在的记录
        const listResponse = await listPrinterSerialNumber({ pageNum: 1, pageSize: 1 });
        if (listResponse.rows && listResponse.rows.length > 0) {
          const record = listResponse.rows[0];
          const response = await delPrinterSerialNumber(record.id);
          this.testResult = JSON.stringify(response, null, 2);
          this.$message.success("删除测试成功");
        } else {
          this.testResult = "没有数据可供删除测试";
        }
      } catch (error) {
        this.testResult = "删除测试失败: " + error.message;
        this.$message.error("删除测试失败");
      }
    }
  }
}
</script>

<style scoped>
.box-card {
  margin-bottom: 20px;
}
pre {
  background-color: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
  max-height: 400px;
  overflow-y: auto;
}
</style>
